package com.xuecheng.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.dto.AddCourseDTO;
import com.xuecheng.dto.EditCourseDTO;
import com.xuecheng.dto.QueryCourseParamsDTO;
import com.xuecheng.exception.XueChengPlusException;
import com.xuecheng.mapper.CourseBaseMapper;
import com.xuecheng.mapper.CourseCategoryMapper;
import com.xuecheng.mapper.CourseMarketMapper;
import com.xuecheng.model.PageParams;
import com.xuecheng.model.PageResult;
import com.xuecheng.po.CourseBase;
import com.xuecheng.po.CourseCategory;
import com.xuecheng.po.CourseMarket;
import com.xuecheng.service.CourseBaseInfoService;
import com.xuecheng.vo.CourseBaseInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 课程信息管理service
 */
@Slf4j
@Service
public class CourseBaseInfoServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseInfoService {
	
	@Resource
	private CourseBaseMapper courseBaseMapper;
	@Resource
	private CourseMarketMapper courseMarketMapper;
	@Resource
	private CourseCategoryMapper courseCategoryMapper;
	@Resource
	private CourseMarketServiceImpl courseMarketService;
	
	
	@Override
	public PageResult<CourseBase> queryCourseBaseList(PageParams params, QueryCourseParamsDTO queryCourseParamsDTO) {
		LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
		//拼接查询条件
		//根据课程名称模糊查询  name like '%名称%'
		queryWrapper.like(StringUtils.isNotEmpty(queryCourseParamsDTO.getCourseName()), CourseBase::getName,
			queryCourseParamsDTO.getCourseName()).eq(StringUtils.isNotEmpty(queryCourseParamsDTO.getAuditStatus()),
			CourseBase::getAuditStatus,
			queryCourseParamsDTO.getAuditStatus()).eq(StringUtils.isNotEmpty(queryCourseParamsDTO.getPublishStatus()),
			CourseBase::getStatus,
			queryCourseParamsDTO.getPublishStatus());
		//分页参数
		Page<CourseBase> page = new Page<>(params.getPageNo(), params.getPageSize());
		//分页查询E page 分页参数, @Param("ew") Wrapper<T> queryWrapper 查询条件
		Page<CourseBase> pageResult = courseBaseMapper.selectPage(page, queryWrapper);
		//准备返回数据 List<T> items, long counts, long page, long pageSize
		return new PageResult<>(pageResult.getRecords(), pageResult.getTotal(), params.getPageNo(),
			params.getPageSize());
	}
	
	
	@Transactional
	@Override
	public CourseBaseInfoVO createCourseBase(Long companyId, AddCourseDTO dto) {

//		校验数据库合法性
		if (StringUtils.isBlank(dto.getName())) {
			//抛出异常
//            throw  new RuntimeException("课程名称为空");
			XueChengPlusException.cast("课程名称为空");
//            XueChengPlusException.cast(CommonError.PARAMS_ERROR);
		}
		
		if (StringUtils.isBlank(dto.getMt())) {
			throw new RuntimeException("课程分类为空");
		}
		
		if (StringUtils.isBlank(dto.getSt())) {
			throw new RuntimeException("课程分类为空");
		}
		
		if (StringUtils.isBlank(dto.getGrade())) {
			throw new RuntimeException("课程等级为空");
		}
		
		if (StringUtils.isBlank(dto.getTeachmode())) {
			throw new RuntimeException("教育模式为空");
		}
		
		if (StringUtils.isBlank(dto.getUsers())) {
			throw new RuntimeException("适应人群为空");
		}
		
		if (StringUtils.isBlank(dto.getCharge())) {
			throw new RuntimeException("收费规则为空");
		}
		
		
		//向Course_base课程基本信息表表添加数据
		CourseBase courseBase = new CourseBase();

//        courseBase.setName(dto.getName());
//        courseBase.setGrade(dto.getGrade());
		//以上设置数据的方法可以拷贝,从源拷贝到目标
		BeanUtils.copyProperties(dto, courseBase);
		//设置机构id
		courseBase.setCompanyId(companyId);
		//创建时间
		courseBase.setCreateDate(LocalDateTime.now());
		//审核状态默认未提交
		courseBase.setAuditStatus("202002");
		//发布状态默认为未发布
		courseBase.setStatus("203001");
		//插入成功返回1
		int insert = courseBaseMapper.insert(courseBase);
		//得到课程id
		Long courseId = courseBase.getId();
		
		//向数据库插入课程基本信息表，拿到课程的id
		//向课程营销表添加数据
		CourseMarket courseMarket = new CourseMarket();
		//两个对象的属性名一致，类型一样
		BeanUtils.copyProperties(dto, courseMarket);
		courseMarket.setId(courseId);
		//校验如果课程为收费，必须输入价格且大于0
//        String charge = courseMarket.getCharge();
//        if(charge.equals("201001")){
//            if(courseMarket.getPrice()==null || courseMarket.getPrice().floatValue()<=0){
////                throw new RuntimeException("课程为收费价格不能为空且必须大于0");
//                XueChengPlusException.cast("课程为收费价格不能为空且必须大于0");
//
//            }
//        }
		//向数据库插入课程营销表
		int insert1 = this.saveCourseMarket(courseMarket);
		//插入成功返回1
//        int insert1 = courseMarketMapper.insert(courseMarket);
//        if(insert<1 && insert1<1){
		//只要有一个插入不成功抛出异常
		if (insert < 1 || insert1 < 1) {
			log.error("创建课程过程中出错:{}", dto);
			throw new RuntimeException("创建课程过程中出错");
		}
		
		//返回
		
		return getCourseBaseInfo(courseId);
	}
	
	/**
	 * 根据课程id查询课程信息，包括基本信息和营销信息
	 */
	public CourseBaseInfoVO getCourseBaseInfo(Long courseId) {
		//课程基本信息
		CourseBase courseBase = courseBaseMapper.selectById(courseId);
		//课程营销信息
		CourseMarket courseMarket = courseMarketMapper.selectById(courseId);
		//组成要返回的数据
		CourseBaseInfoVO courseBaseInfoVO = new CourseBaseInfoVO();
		BeanUtils.copyProperties(courseBase, courseBaseInfoVO);
		if (courseMarket != null) {
			BeanUtils.copyProperties(courseMarket, courseBaseInfoVO);
		}
		
		//向分类的名称查询出来
		CourseCategory courseCategory = courseCategoryMapper.selectById(courseBase.getMt());//一级分类
		courseBaseInfoVO.setMtName(courseCategory.getName());
		CourseCategory courseCategory2 = courseCategoryMapper.selectById(courseBase.getSt());//二级分类
		courseBaseInfoVO.setStName(courseCategory2.getName());
		
		return courseBaseInfoVO;
	}
	
	@Override
	public CourseBaseInfoVO updateCourseBase(Long companyId, EditCourseDTO dto) {
		
		//校验
		//课程id
		Long id = dto.getId();
		CourseBase courseBase = courseBaseMapper.selectById(id);
		if (courseBase == null) {
			XueChengPlusException.cast("课程不存在");
		}
		
		//校验本机构只能修改本机构的课程
		if (!courseBase.getCompanyId().equals(companyId)) {
			XueChengPlusException.cast("本机构只能修改本机构的课程");
		}
		
		//封装基本信息的数据
		BeanUtils.copyProperties(dto, courseBase);
		courseBase.setChangeDate(LocalDateTime.now());
		
		//更新课程基本信息
		int i = courseBaseMapper.updateById(courseBase);
		
		//封装营销信息的数据
		CourseMarket courseMarket = new CourseMarket();
		BeanUtils.copyProperties(dto, courseMarket);
		
		//校验如果课程为收费，必须输入价格且大于0
//        String charge = courseMarket.getCharge();
//        if(charge.equals("201001")){
//            if(courseMarket.getPrice()==null || courseMarket.getPrice().floatValue()<=0){
////                throw new RuntimeException("课程为收费价格不能为空且必须大于0");
//                XueChengPlusException.cast("课程为收费价格不能为空且必须大于0");
//
//            }
//        }
		
		//请求数据库
		//对营销表有则更新，没有则添加
//        boolean b = courseMarketService.saveOrUpdate(courseMarket);
		
		saveCourseMarket(courseMarket);
		//查询课程信息
		return getCourseBaseInfo(id);
	}
	
	//抽取对营销的保存
	private int saveCourseMarket(CourseMarket courseMarket) {
		
		
		String charge = courseMarket.getCharge();
		if (StringUtils.isBlank(charge)) {
			XueChengPlusException.cast("收费规则没有选择");
		}
		if (charge.equals("201001")) {
			if (courseMarket.getPrice() == null || courseMarket.getPrice().floatValue() <= 0) {
//                throw new RuntimeException("课程为收费价格不能为空且必须大于0");
				XueChengPlusException.cast("课程为收费价格不能为空且必须大于0");
				
			}
		}
		
		//保存
		boolean b = courseMarketService.saveOrUpdate(courseMarket);
		return b ? 1 : 0;
		
	}
	
}
